<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .container{
            width: 1000px;
            margin: auto;
        }
        .header{
            text-align: center;
        }
        .h1{
            text-align: center;
            padding: 50px 0;
            background-color: #eee;
            margin-bottom: 20px;
        }
        h1{
            height: 13px;
        }
        input{
            height: 17.8px;
            margin: 10px;
            width: 180px;
        }
        select,button{
            width: 180px;
            height: 23px;
        }
        .main p{
            border-top: 1px solid #CECECE;
            -webkit-margin-before: 0;
            -webkit-margin-after: 0;
            -webkit-margin-start: 0;
            -webkit-margin-end: 0;
            line-height: 35px;
            padding-left: 10px;
        }
        .main > p span:nth-of-type(1){
            width: 40%;
            display: inline-block;

        }
        .main > p span:nth-of-type(2){
            width: 30%;
            display: inline-block;

        }
        .main > p span:nth-of-type(3){
            width: 30%;
            display: inline-block;

        }
        .main .con > p span:nth-of-type(1){
            width: 41%;
            display: inline-block;


        }
        .main .con > p span:nth-of-type(2){
            width: 29%;
            display: inline-block;

        }
        .main .con > p span:nth-of-type(3){
            width: 30%;
            display: inline-block;

        }
        .con{
            height: 178px;
            overflow-x: hidden;
            overflow-y: auto;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="h1">
        <h1>学员列表</h1>
        <span>1804</span>
    </div>


    <div class="header">

        <input type="text" placeholder="姓名" id="n">
        <select name="" id="s">
            <option>男</option>
            <option>女</option>
        </select>
        <input type="number" id="a" placeholder="年龄">
        <button id="btn">提交</button>

    </div>
    <div class="main">
        <h2>列表</h2>
        <p><span>姓名</span><span>性别</span><span>年龄</span></p>
        <div class="con"></div>
    </div>
</div>


<script>









    function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                console.log(xmlhttp.responseText);

                var json = JSON.parse(xmlhttp.responseText);
                var con = document.querySelector(".con");
                con.innerHTML = "";
                for (var i = 0; i < json.data.length; i++) {
                    var p = document.createElement('p');
                    var span1 = document.createElement("span");
                    var span2 = document.createElement("span");
                    var span3 = document.createElement("span");
                    span1.innerHTML = json.data[i].studentName;
                    span2.innerHTML = json.data[i].sex;
                    span3.innerHTML = json.data[i].age;

                    p.appendChild(span1);
                    p.appendChild(span2);
                    p.appendChild(span3);
                    document.querySelector(".con").appendChild(p)
                }


            }
        }
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
        xmlhttp.send();
    }



    loadXMLDoc()





    document.querySelector("#btn").addEventListener('click',function () {
        var n = document.querySelector("#n").value;
        var s = document.querySelector("#s").value;
        var a = document.querySelector("#a").value;
        console.log(n + " " + s + " " + a);


        var xmlhttp;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // IE6, IE5 浏览器执行代码
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);
                var json = JSON.parse(xmlhttp.responseText)


                loadXMLDoc()
                var n = document.querySelector("#n");
                var a = document.querySelector("#a");
                a.value= "";
                n.value = "";
            }
        };
        xmlhttp.open("GET", "http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+ n+"&sex="+s+"&age="+a, true);
//        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
        xmlhttp.send();

    })





</script>
</body>
</html>